--[[
FilePath: \Lua\View\UITotalRecharge\UITotalRecharge.lua
累充活动
--]]

require 'View/UIBase/UIBase'

UITotalRecharge = UIBase.New(PanelResNames.UITotalRecharge)
local _M = UITotalRecharge

_M.hasInit = false
_M.activeId = nil
_M.playEffect = false

function _M.Init(func, id)
    _M.activeId = id
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("bgClose"), self.OnClose)
        self.bg = self:GetChild("bg");
        self.name = self:GetChild("name");
        self.itemList = self:GetChild("Itemlist");
        self.go = self:GetChild("go");
        self.get = self:GetChild("get");
        self.tipTime = self:GetChild("tipTime");
        --self.tipDecs = self:GetChild("tipDecs");
        self.tipEndDecs = self:GetChild("tipDecs2");
        self.tipsRecharge = self:GetChild("TipsRecharge");
        self.tipsNum = self:GetChild("TipsNum");
        self.isGetController = self:GetController("IsGet");
        self.isOver = self:GetController("isover");
        self.btnDetail = self:GetChild("Button");
        local activeListTableData = G.dataTable["t_activitylist"];
        self.modelData = G.dataTable[activeListTableData[tostring(self.activeId)].f_ModelName][tostring(self.activeId)];
        self.itemConfig = G.dataTable[tostring(self.modelData.f_ItemTableName)];
        self.itemList.itemRenderer = self.RefreshItem;

        self:addEvent()
        self:InitView()

        _M.hasInit = true
    end
end

function _M:addEvent()
    self.go.onClick:Add(self.ClickGoto);
    self.get.onClick:Add(self.ClickReward);
    self.btnDetail.onClick:Add(self.ClickDetail);
    self:RegisterEvent(Config.EventType.Fresh_Active,self.RefreshActive);
end

function _M:removeEvent()
    self.go.onClick:Remove(self.ClickGoto);
    self.get.onClick:Remove(self.ClickReward);
    self.btnDetail.onClick:Remove(self.ClickDetail);
end

function _M.ClickGoto()
    logicMgr.HeroManager.setGetNewHeroShow();
    closeUI(_M._panelName);
    closeUI(PanelNames.UIActivity);
    showUI(PanelNames.UIHeadMenu);
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Cost, true) then
        logicMgr.CostManager.gotoCostById();
    end
end

function _M.ClickReward()
    if _M.isGet then
        return;
    end

    _M.isGet = true;
    ActivityHandler.sendReqActivityDrawRewardMessage({_M.activeId, _M.serverData.itemId});
end

function _M.ClickDetail()
    showUI(PanelNames.UITotalRechargeShow, nil, _M.activeId);
end

function _M:InitView()
    if self.activeId == nil then
        self:OnBack();
    else
        if self.modelData ~= nil then
            if self.modelData.f_Banner ~= nil and self.modelData.f_Banner ~= "" then
                self.bg.url = tostring(self.modelData.f_Banner);
            end
            if self.modelData.f_TitlePic ~= nil and self.modelData.f_TitlePic ~= "" then
                self.name.url = tostring(self.modelData.f_TitlePic);
            end
            --self.tipDecs.text = getLanguage("后结束");
            self.tipEndDecs.text = getLanguage("Sys_ActivityEnd");
            self.tipsRecharge.text = getLanguage("Sys_TotalRecharge");
        end
    end
end

function _M:RefreshActive()
    if self.activeId == nil or not _M.hasInit then
        self:OnBack();
    else
        self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
        if self.serverData == nil then
            self:OnBack();
            return;
        end
        self:RefreshView();
    end
end

function _M:RefreshView()
    local config = self.itemConfig[tostring(self.serverData.itemId)];
    local state = tonumber(self.serverData.state);
    self.isGetController.selectedIndex = state;
    if config == nil then
        self.itemList.numItems = 0;
        self.tipsNum.text = self.serverData.num .. "/" .. self.serverData.num;
    else
        self.showType = tonumber(config.f_ShowType);
        self.reward = decodeJsonStrToTabel(config.f_Award);
        self.itemList.numItems = #self.reward;
        if state == 0 then
            self.tipsNum.text = "[color=#FF2A4D]"..self.serverData.num.."[/color]/"..config.f_Price;
        elseif state == 1 then
            self.tipsNum.text = "[color=#A0C776]"..config.f_Price.."[/color]/"..config.f_Price;
        else
            self.tipsNum.text = config.f_Price.."/"..config.f_Price;
        end

    end
    if self.playEffect then
        self.playEffect = false;
        HelperFunc.ListTranstionPlay(self.itemList);
    end
    self.isGet = false;
    local time = HelperFunc.GetServerTime();
    self.endCountDown = self.serverData.endTime - time;
    self:updateActiveCountDown();
    self:TimeCall(self.updateActiveCountDown, 1000, -1);
end

function _M.RefreshItem(index, itemObj)
    local config = _M.reward[tonumber(index) + 1];
    local itemContent = itemObj:GetController("Item");
    local title = itemObj:GetChild("title");
    itemContent.selectedIndex = _M.showType;
    local itemConfig = G.dataTable["t_item"][tostring(config[1])];
    if _M.showType ~= nil and _M.showType == 1 then
        local item = itemObj:GetChild("Item");
        --logicMgr.CostManager.RefreshItem(item, config, {isShowBigNum = true})
        logicMgr.CostManager.RefreshItem(item, config)
    else
        local icon = itemObj:GetChild("ItemIcon");
        icon.url = EnumConst.AtlasPath.ItemAtlas .. itemConfig.f_Icon;
    end
    title.text = getLanguage(itemConfig.f_ItemName).."x"..getNumString(tonumber(config[2]));
end

function _M:updateActiveCountDown()
    if self.endCountDown == nil then
        return;
    end
    if self.endCountDown <= 0 then
        self.isOver.selectedIndex = 1;
        self:RemoveTimeCall(self.updateActiveCountDown)
    else
        self.tipTime.text = string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetSurpTimeStr(self.endCountDown));
    end
    self.endCountDown = self.endCountDown - 1;
end

-- 显示
function _M:OnEnable()
    self.playEffect = true;
    self:RefreshActive();
    logicMgr.HeroManager.setGetNewHeroShow(true);
end

function _M:OnClose()
    self:OnBack();
    logicMgr.HeroManager.setGetNewHeroShow();
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:RemoveTimeCall(self.updateActiveCountDown)
end

return _M;